﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Repositories.Interfaces;
using Repositories;

namespace AsiaSnackExpress
{
    public partial class Login : System.Web.UI.Page
    {
        #region Properties


        IUserLoginRepository _userLoginRep;
        public IUserLoginRepository UserLoginRep
        {
            get
            {
                if (_userLoginRep == null)
                {
                    _userLoginRep = DIHelper.Resolve<IUserLoginRepository>();
                }
                return _userLoginRep;
            }

        }


        #endregion

        protected override void OnLoad(EventArgs e)
        {
            DIHelper.Configure();
            base.OnLoad(e);
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            this.Form.DefaultButton = loginPanel.FindControl("LoginButton").UniqueID;
            //Check loock in
            if (!Page.IsPostBack)
            {

            }
        }


        protected void loginPanel_Authenticate(object sender, AuthenticateEventArgs e)
        {
            bool isLogin = UserLoginRep.FindByUserName(loginPanel.UserName, loginPanel.Password);
            if (isLogin)
            {
                Session["userName"] = loginPanel.UserName;
                if (loginPanel.RememberMeSet)
                {
                    Session["password"] = loginPanel.Password;
                }
            }
            else
            {
                Session["userName"] = null;
            }
            e.Authenticated = isLogin;
        }

    }
}